package b4.view
{
	import b4.model.SmartBirdExtension;
	import b4.view.component.ChatDialog;
	
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.smartbird.Message;

	public class ChatDialogMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'ChatDialogMediator';
		public static const GET_NEW_MESSAGE:String = NAME + "GetNewMessage"; 
		
		public function ChatDialogMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				ChatDialogMediator.GET_NEW_MESSAGE
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			
			switch ( name )
			{
				case ChatDialogMediator.GET_NEW_MESSAGE:
					chatDialog.addNewMessage(smartBirdExtension.vo.newMessage);
				break;
			}
		}
		
		override public function onRegister():void
		{
			sendNotification(ApplicationMediator.ADD_CHILD, viewComponent);
			chatDialog.sendButton.addActionListener(onSendButtonClick);
		}
		
		private function onSendButtonClick(event:Event):void
		{
			var idx:int = chatDialog.selectdIdx;
			smartBirdExtension.sendMessage(chatDialog.inputField.getText(), Message.TYPE_WORLD); 
		}
		
		override public function onRemove():void
		{
		}
		
		private function get chatDialog():ChatDialog
		{
			return viewComponent as ChatDialog;
		}
		
		private function get smartBirdExtension():SmartBirdExtension
		{
			return facade.retrieveProxy( SmartBirdExtension.NAME ) as SmartBirdExtension;
		}
	}
}